package com.example.yiwei.crm.view.activity.clueActivity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.clueDao;
import com.example.yiwei.crm.data.GetClue;
import com.example.yiwei.crm.data.GetClueData;
import com.example.yiwei.crm.dao_impl.clueDaoImpl;
import com.example.yiwei.crm.entity.ClueMeesageEntity;
import com.example.yiwei.crm.view.Class.listviewClass.AutoListView;

import java.util.ArrayList;
import java.util.List;

public class ClueSimpleMessageActivity extends AppCompatActivity implements AutoListView.OnRefreshListener{
    private AutoListView lstv;
    MyAdapter adapter;
    private TextView txt_title;
    List<ClueMeesageEntity> clueList;
    List<ClueMeesageEntity> clue;
    Intent intent;
    ImageView img_back,img_plus,img_search;
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            List<ClueMeesageEntity> result = (List<ClueMeesageEntity>) msg.obj;
            switch (msg.what) {
                case AutoListView.REFRESH:
                    lstv.onRefreshComplete();
                    clueList.clear();
                    clueList.addAll(result);
                    break;
                case AutoListView.LOAD:
                    lstv.onLoadComplete();
                    clueList.addAll(result);
                    break;
            }
//            lstv.setResultSize(result.size());
            adapter.notifyDataSetChanged();

        }

        ;
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clue_simple_message);
        clueList=new ArrayList<>();
        txt_title= (TextView) findViewById(R.id.clue_simple_txt_title);
        img_back= (ImageView) findViewById(R.id.clueSimpleMessage_btn_back);
        img_search= (ImageView) findViewById(R.id.clueSimpleMessage_img_search);
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        img_plus= (ImageView) findViewById(R.id.clueSimpleMessage_btn_plus);
        img_plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(ClueSimpleMessageActivity.this,ClueAddMessageActivity.class);
                if ("xiansuochi".equals(getIntent().getStringExtra("xiansuochiTag"))){
                    intent.putExtra("xiansuochiTag","xiansuochi");
                }
                startActivity(intent);
            }
        });
        if (getIntent().getStringExtra("xiansuochiTag")!=null){
            if ("xiansuochi".equals(getIntent().getStringExtra("xiansuochiTag"))){
                txt_title.setText("线索池");
            }
        }
        img_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(ClueSimpleMessageActivity.this,ClueSearchMessageActivity.class);
                if ("xiansuochi".equals(getIntent().getStringExtra("xiansuochiTag"))){
                    intent.putExtra("xiansuochiTag","xiansuochi");
                }
                startActivity(intent);
            }
        });
        lstv= (AutoListView) findViewById(R.id.clueSimpleMessage_listview);
        lstv.setDivider(new ColorDrawable(Color.rgb(216,216,216)));
        lstv.setDividerHeight(1);
        adapter = new MyAdapter();
        lstv.setAdapter(adapter);
        lstv.setOnRefreshListener(this);
        lstv.setLoadEnable(false);
        new Thread(new Runnable() {
            @Override
            public void run() {
                clueDao clueDao=new clueDaoImpl();
                if (getIntent().getStringExtra("xiansuochiTag")!=null){
                    if ("xiansuochi".equals(getIntent().getStringExtra("xiansuochiTag"))){
                        clueList=clueDao.querySimpleClueMessage(baseClass.clue_group_url,null);
                    }
                }else {
                    clueList = clueDao.querySimpleClueMessage(baseClass.query_all_clue_message_ul, null);
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.notifyDataSetChanged();
                        lstv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                final int clue_id=clueList.get(clueList.size()-i).getClue_id();
                                intent=new Intent(ClueSimpleMessageActivity.this,ClueDetailedMessageActivity.class);
                                intent.putExtra("id",clue_id);
                                if ("xiansuochi".equals(getIntent().getStringExtra("xiansuochiTag"))){
                                    intent.putExtra("id",clue_id);
                                    intent.putExtra("xiansuochiTag","xiansuochi");
                                }
                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        clueDao cluedao1=new clueDaoImpl();
                                        GetClue.clue= cluedao1.queryDetailedClueMessage(baseClass.query_detailed_clue_message_url+clue_id, null);
                                        if ("success".equals(GetClue.clue.getTag())){
                                            startActivity(intent);

                                        }else{
                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    Toast.makeText(ClueSimpleMessageActivity.this, "数据加载失败", Toast.LENGTH_SHORT).show();
                                                }
                                            });
                                        }

                                    }
                                }).start();
                            }
                        });
                    }
                });
            }
        }).start();
    }
    private void loadData(int refresh) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Message msg = handler.obtainMessage();
                int what = 0;
                msg.what = what;
                msg.obj = getData();
                if (msg.obj!=null) {
                    handler.sendMessage(msg);
                }else{
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(ClueSimpleMessageActivity.this, "网络不给力啊！请重试", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        }).start();
    }
    public void onRefresh() {
        loadData(AutoListView.REFRESH);
    }
    public List<ClueMeesageEntity> getData() {
        clue = null;
        new Thread(new Runnable() {
            @Override
            public void run() {
                clueDao cluedao = new clueDaoImpl();
                clue = cluedao.querySimpleClueMessage(baseClass.query_all_clue_message_ul,null);

            }
        }).start();
        if (clue!=null){
            return clue;
        }else{
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return clue;
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (GetClueData.clue!=null){
            clueList.clear();
            clueList.addAll(GetClueData.clue);
            adapter.notifyDataSetChanged();
            GetClueData.clue=null;
        }
    }
    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return   clueList.size();
        }

        @Override
        public Object getItem(int i) {
            return i;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
           ViewHolder holder;
            if (view==null){
                view=getLayoutInflater().inflate(R.layout.listview_contact,null);
                holder=new ViewHolder();
                holder.txt_name= (TextView) view.findViewById(R.id.listview_contact_txt_name);
                holder.txt_phone= (TextView) view.findViewById(R.id.listview_contact_txt_mobile_phone);
                view.setTag(holder);
            }else{
                holder= (ViewHolder) view.getTag();
            }
            holder.txt_name.setText(clueList.get(clueList.size()-i-1).getClue_name());
            holder.txt_phone.setText(clueList.get(clueList.size()-i-1).getClue_mobile_phone());
            return view;
        }
        class ViewHolder{
            TextView txt_name,txt_phone;
        }
    }
}
